import java.util.Random;

public class MonopolyBoard {
	private Deeds[] deeds;
	private Players[] player;
	Dice dice = new Dice();
	int turn = 0;
	int jailCounter = 0;
	int previousTurn = 0;
	private Tiles[] tile;
	private CommunityChest chest;
	private Chance chance;

	public MonopolyBoard(Tiles[] tile, Deeds[] deeds) {
		this.tile = tile;
		this.deeds = deeds;

	}

	public void createPlayer(int x) {
		player = new Players[x];
		for (int i = 0; i < x; i++) {
			int playerNum = i + 1;
			String name = "Player " + playerNum;
			player[i] = new Players(this.deeds);
			player[i].setName(name);
		}
		chance = new Chance(player);
		chest = new CommunityChest(player);
	}

	public Players[] getPlayers() {
		return player;
	}

	public int getTurn() {
		return turn;
		// return player[turn].getToken();
	}

	
	public void nextTurn(int dice1, int dice2) {
		if (this.isJail() == false) {
			if (dice1 != dice2) {
				if (turn == player.length - 1) {
					turn = 0;
				} else {
					turn++;
				}
				jailCounter=0;
			}
		 } else {
			 if(dice1==dice2){
				player[turn].setGetOutOfJail(0);
			 }
			if (turn == player.length - 1) {
				turn = 0;
			} else {
				turn++;
			}
			jailCounter=0;

		
		}
		previousTurn = turn;
	}
	

	public void setToken(int x) {

		{
			for (int i = 0; i < player.length; i++) {
				if (player[i].getToken() == 0) {
					player[i].setToken(x);
					i = player.length;
				}
			}
		

		}
	}

	public boolean sendToJail() {
		if (player[turn].getPosition() == 30 || jailCounter == 3) {
			this.jailCounter = 0;
			player[turn].setGetOutOfJail(2);
			return true;
		} else {
			return false;
		}
	}

	public void ThreeDiceChecker(int dice1, int dice2) {
		if(player[this.turn].getIsJail()==true&&dice1==dice2){
		player[this.turn].setGetOutOfJail(0);
		}else{
		if (dice1==dice2&&this.isJail()==false) {
			jailCounter++;
		} else {
			jailCounter = 0;
		}
		}
	}

	public boolean isJail() {
		if (player[this.turn].getIsJail() == true) {
			return true;
		} else {
			return false;
		}
	}
	public void Chance(int x){

		chance.generateCard(x,this.turn);
	}
	
	public void Community(int x){
		chest.generateCard(x, this.turn);
	}
	
	public void rent(){
		int i;
		int j;
		for( i=0;i<deeds.length;i++){
			if(deeds[i].getPosition()==player[this.turn].getPosition()){
			break;
			}
		}
		for( j=0;j<player.length;j++){
			if(deeds[i].getOwner()==player[j].getName()){
				if(player[j].getName()!=player[this.turn].getName()){
					player[j].setMoney(deeds[i].getRent());
				}
				break;
			}
		}

		
		player[this.turn].rent();
	}

}
